﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;

namespace AppCompress.Model
{
    public class FileSystemItem
    {
        public string Name { get; set; }
        public DateTime EditTime { get; set; }
        public bool IsFolder { get; set; }
        public string FullName { get; set; }
        public string Extension { get; set; }
        public List<FileSystemItem> ChildrenFileSystemItems { get; set; }
        public string ParentPath { get; set; }

        public FileSystemItem()
        {
        }

        public FileSystemItem(string name, DateTime editTime, bool isFolder, string fullName,
            List<FileSystemItem> childrenFileSystemItems)
        {
            Name = name;
            EditTime = editTime;
            IsFolder = isFolder;
            FullName = fullName;
            Extension = Path.GetExtension(fullName);

            ChildrenFileSystemItems = childrenFileSystemItems;
        }

        /// <summary>
        /// 如果没有访问权限 会出现异常，在使用该方法时，进行异常捕获和处理。
        /// </summary>
        /// <param name="fullPath"></param>
        /// <returns></returns>
        public List<FileSystemItem> GetFileSystemItems(string fullPath)
        {
            DirectoryInfo directoryInfo = new DirectoryInfo(fullPath);
            List<FileSystemItem> fileSystemItems = new List<FileSystemItem>();
            fileSystemItems.AddRange(directoryInfo.GetDirectories().Select(d =>
                new FileSystemItem()
                {
                    Name = d.Name,
                    EditTime = d.LastWriteTime,
                    IsFolder = Directory.Exists(d.FullName),
                    FullName = d.FullName,
                    Extension = "文件夹",
                    ChildrenFileSystemItems = GetFileSystemItems(d.FullName),
                    ParentPath = d.FullName.Remove(d.FullName.LastIndexOf(Path.DirectorySeparatorChar)),
                }).ToList());
            fileSystemItems.AddRange(directoryInfo.GetFiles().Select(f => new FileSystemItem()
            {
                Name = f.Name,
                EditTime = f.LastWriteTime,
                FullName = f.FullName,
                IsFolder = Directory.Exists(f.FullName),
                Extension = Path.GetExtension(f.FullName),
                ParentPath = f.FullName.Remove(f.FullName.LastIndexOf(Path.DirectorySeparatorChar)),
            }).ToList());
            return fileSystemItems;
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="fullPath"></param>
        /// <returns></returns>
        public List<FileSystemItem> GetParentFolder(string fullPath)
        {
            List<FileSystemItem> parentFileSystemItems = new List<FileSystemItem>();
            DirectoryInfo directoryInfo = new DirectoryInfo(fullPath);
            parentFileSystemItems.Add(new FileSystemItem(directoryInfo.Name, directoryInfo.LastWriteTime, Directory.Exists(fullPath), directoryInfo.FullName, GetFileSystemItems(fullPath)));
            return parentFileSystemItems;
        }
    }
}